/**
 * 用户信息
 * @type {Vue}
 */
var user_info = new Vue({
    el: "#userInfo",
    mounted: function () {
        this.checkLogin();//需要触发的函数
    },
    data: {
        nickname: handleLocalStorage("get", "nickname"),
        file: [],
        userInfo: []
    },
    methods: {
        checkLogin: function () {
            if (this.nickname == '') {
                alert("当前未登录！");
                window.location.href = "/user/login-register.html";
            } else {
                $.ajax({
                    url: "/user/userInfo",
                    type: "GET",
                    headers: {
                        "Authorization": "Bearer " + handleLocalStorage("get", "token")
                    },
                    dataType: "json",
                    data: {},
                    success: function (response) {
                        if (response.code == 200) {
                            user_info.userInfo = response.data;
                            if (user_info.userInfo.addresses == null) {
                                user_info.userInfo.addresses = 1;
                            }
                        } else {
                            alert(response.message);
                        }
                    },
                    error: function (error) {
                        alert("服务器错误");
                    }
                });
            }
        },
        Logout: function () {
            handleLocalStorage("remove", "token");
            handleLocalStorage("remove", "nickname");
            window.location.href = "/";
        },
        saveInfo: function () {
            if (this.nickname == "") {
                alert("昵称不能为空! ");
                return false;
            }
            let formData = new FormData();
            formData.append('nickname', this.nickname);
            formData.append('file', this.$refs.file.files[0]);
            $.ajax({
                url: "/user/saveInfo",
                type: "POST",
                headers: {
                    "Authorization": "Bearer " + handleLocalStorage("get", "token")
                },
                dataType: "json",
                processData: false,
                contentType: false,
                data: formData,
                success: function (response) {
                    if (response.code == 200) {
                        alert(response.message);
                    } else {
                        alert(response.message);
                    }
                },
                error: function (error) {
                    alert("服务器错误");
                }
            });

        },
        getAddress: function (id) {
            var addresses = user_info.userInfo.addresses;
            for (var i = 0; i < addresses.length; i++) {
                if (addresses[i].id == parseInt(id)) {
                    edit_address.address = addresses[i];
                    edit_address.flag = 1;
                }
            }

        },
        delAddress: function (id) {
            alert("确认删除吗");
            $.ajax({
                url: "/user/address",
                type: "POST",
                headers: {
                    "Authorization": "Bearer " + handleLocalStorage("get", "token")
                },
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                    "id": id,
                    "province":"delete"
                }),
                success: function (response) {
                    if (response.code == 200) {
                        user_info.checkLogin();
                    } else {
                        alert(response.message);
                    }
                },
                error: function (error) {
                    alert("服务器错误");
                }
            });
        },
        savePassword: function () {
            var old_password = $("#current-pwd").val();
            var new_password = $("#new-pwd").val();
            var new_password2 = $("#confirm-pwd").val();
            if (new_password != new_password2) {
                alert("两次输入不一致");
                return;
            }
            $.ajax({
                url: "/user/modify_password",
                type: "POST",
                headers: {
                    "Authorization": "Bearer " + handleLocalStorage("get", "token")
                },
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                    "old_password": old_password,
                    "new_password": new_password
                }),
                success: function (response) {
                    if (response.code == 200) {
                        alert(response.message);
                        window.location.reload();
                    } else {
                        alert(response.message);
                    }
                },
                error: function (error) {
                    alert("服务器错误");
                }
            });
        },
    }
});

var edit_address = new Vue({
    el: "#edit_address",
    data: {
        address: '',
        flag: 0
    },
    methods: {
        updateAddress: function () {
            var province = $("#province").find("option:selected").text();
            var city = $("#city").find("option:selected").text();
            var area = $("#area").find("option:selected").text();
            var street = $("#street").val();
            var consignee = $("#consignee").val();
            var phone = $("#phone").val();
            if (street == '') {
                alert("请正确填写地址！");
                return;
            }
            $.ajax({
                url: "/user/address",
                type: "POST",
                headers: {
                    "Authorization": "Bearer " + handleLocalStorage("get", "token")
                },
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                    "id": edit_address.address.id,
                    "consignee": consignee,
                    "phone": phone,
                    "province": province,
                    "city": city,
                    "area": area,
                    "street": street,
                }),
                success: function (response) {
                    if (response.code == 200) {
                        alert(response.message);
                    } else {
                        alert(response.message);
                    }
                },
                error: function (error) {
                    alert("服务器错误");
                }
            });
        },
        refresh: function () {
            user_info.checkLogin();
        }
    }
});
